package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SalChance;
import cn.com.bdqn.entity.SysMenu;
import cn.com.bdqn.entity.SysUser;
import cn.com.bdqn.service.SysMenuService;
import cn.com.bdqn.utils.JwtUtils;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/sysMenu")
@Api(tags = "菜单管理接口")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @Operation(summary = "根据登录用户权限查询应有菜单")
    @PostMapping("/queryMenu")
    public ReturnData queryMenu(String jwt){

        //  对JWT进行验证，并返回其中的Claims对象
        Claims  claims  =  JwtUtils.verifyJwt(jwt);

        //  从Claims对象中获取用户角色ID并打印输出
        System.out.println(claims.get("usrRoleId"));

        //  根据用户角色ID查询该角色所拥有的顶级菜单列表
        List<SysMenu>  sysMenus  =  sysMenuService.queryMenuByRoleId(Long.parseLong(claims.get("usrRoleId").toString())  ,  0L);

        //  遍历顶级菜单列表，对每个菜单再次查询其下属菜单列表，并将其设置到当前菜单对象的menus属性中
        for  (SysMenu  sysMenu  :  sysMenus)  {
            Long  menuCode  =  sysMenu.getMenuCode();
            List<SysMenu>  sysMenus1  =  sysMenuService.queryMenuByRoleId(Long.parseLong(claims.get("usrRoleId").toString()),  menuCode);
            sysMenu.setMenus(sysMenus1);
        }

        //  返回一个包含菜单列表的ReturnData对象
        return  ReturnData.ok().put("menu",sysMenus);
    }

    @Operation(summary = "查询所有菜单")
    @PostMapping("/queryAll")
    public ReturnData queryAll(Long rfRoleId){
        //  查询所有顶级菜单列表
        List<SysMenu> sysMenus = sysMenuService.queryAll(rfRoleId,0L);

        //  遍历顶级菜单列表，对每个菜单再次查询其下属菜单列表，并将其设置到当前菜单对象的menus属性中
        for  (SysMenu  sysMenu  :  sysMenus)  {
            Long  menuCode  =  sysMenu.getMenuCode();
            List<SysMenu>  sysMenus1  =  sysMenuService.queryAll(rfRoleId,menuCode);
            sysMenu.setMenus(sysMenus1);
        }

        //  返回一个包含菜单列表的ReturnData对象
        return  ReturnData.ok().put("menu",sysMenus);
    }

    @Operation(summary = "菜单高级查询")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(SysMenu sysMenu, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "15") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<SysMenu> sysMenus = sysMenuService.queryByLike(sysMenu);
            PageInfo pageInfo = new PageInfo(sysMenus,3);
            return ReturnData.ok().put("sysMenus",sysMenus).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("菜单高级查询异常");
        }
    }

    @Operation(summary = "查询菜单父节点")
    @PostMapping("/queryParent")
    public ReturnData queryParent(){
        try {
            List<SysMenu> sysMenus = sysMenuService.queryParent();
            return ReturnData.ok().put("sysMenus",sysMenus);
        }catch (Exception e){
            return ReturnData.currError("查询菜单父节点异常");
        }
    }

    @Operation(summary = "添加菜单")
    @PostMapping("/addSysMenu")
    public ReturnData addSysMenu(SysMenu sysMenu){
        int result = sysMenuService.addSysMenu(sysMenu);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }
    }

    @Operation(summary = "根据菜单编号查询菜单信息")
    @PostMapping("/queryParent")
    public ReturnData queryParent(Long menuCode){
        try {
            SysMenu sysMenu = sysMenuService.queryByMenuCode(menuCode);
            return ReturnData.ok().put("sysMenu",sysMenu);
        }catch (Exception e){
            return ReturnData.currError("查询异常");
        }
    }

    @Operation(summary = "修改菜单")
    @PostMapping("/updateSysMenu")
    public ReturnData updateSysMenu(SysMenu sysMenu){
        int result = sysMenuService.updateSysMenu(sysMenu);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.error();
        }
    }

}
